package com.example.front.mapper;

import com.example.common.dto.Pagination;
import com.example.front.entity.Department;
import com.example.front.entity.Equipment;
import com.example.passport.dto.WxUserDto;
import com.example.passport.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DepartmentMapper {


    @Select("<script>" +
            "SELECT count(*) FROM `department` " +
            "<where>" +
            "<if test='condition.id!=null'> AND `id`=#{condition.id}</if>" +
            "<if test='condition.name!=null'> AND `name` LIKE CONCAT('%',#{condition.name},'%')</if>" +
            " AND `tenant_id`=#{condition.tenantId} " +
            "</where> AND `status`!=" + User.STATUS_DELETE +
            "</script>")
    int count(@Param("condition") Department condition);

    @Select("<script>" +
            "SELECT * FROM `department`" +
            "<where>" +
            "<if test='condition.id!=null'> AND `id`=#{condition.id}</if>" +
            "<if test='condition.name!=null'> AND `name` LIKE CONCAT('%',#{condition.name},'%')</if>" +
            " AND `tenant_id`=#{condition.tenantId} " +
            "</where> AND `status`!=" + User.STATUS_DELETE +
            " ORDER BY `id` DESC " +
            "<if test='pagination.limit!=0'> LIMIT #{pagination.offset}, #{pagination.limit}</if>" +
            "</script>")
    List<Department> list(@Param("condition") Department department, @Param("pagination") Pagination pagination);


    @Select("<script>" +
            "SELECT * FROM `department`" +
            "<where>" +
            "<if test='condition.id!=null'> AND `id`=#{condition.id}</if>" +
            "<if test='condition.name!=null'> AND `name` LIKE CONCAT('%',#{condition.name},'%')</if>" +
            " AND `tenant_id`=#{condition.tenantId} " +
            "</where> AND `status`!=" + User.STATUS_DELETE +
            "</script>")
    Department find(@Param("condition") Department department);

    @Insert("<script>" +
            "INSERT INTO `department` " +
            " (`tenant_id`" +
            " <if test='condition.name!=null'> ,`name`</if>" +
            " )" +
            " VALUES " +
            " (#{condition.tenantId}" +
            " <if test='condition.name!=null'> ,#{condition.name} </if>" +
            " )"+
            "</script>")
    int create(@Param("condition") Department department);

    @Update("UPDATE department SET " +
            "`name` = #{condition.name}" +
            " WHERE `id` = #{condition.id} AND `tenant_id` = #{department.tenantId}")
    int update(@Param("condition") Department department);

    @Update("UPDATE `department` SET `status` ="+ User.STATUS_DELETE+
            " WHERE `id`=#{id}")
    int delete(@Param("id") long id);
}
